<template>
  <div class="job-list">
    <div
      class="job-item clearfix"
      v-for="(j, i) in jobList"
      :key="'positon_'+j.id"
      @click="toCloseMenu"
    >
      <div class="job-pos">
        <div class="text">
          <span class="job-name">{{ j.name }}</span>
          <span class="money" v-if="j.payid">{{ j.payid }}-{{ j.pay_end }}k</span>
        </div>
        <div class="wrap">
          <p>
            <i class="iconfont icon-map"></i><span>{{ j.city_name }}</span>
          </p>
          <p>
            <i class="iconfont icon-edit"></i><span>{{ j.is_all === 0 ? '实习' : '全职' }}</span>
          </p>
          <p>
            <i class="iconfont icon-hat"></i><span>{{ j.eid }}</span>
          </p>
        </div>
      </div>
      <div class="resume-num">收到{{ j.apply_num }}份简历</div>
      <div class="toggle-switch" @click.stop>
        <label>
          <span class="show-btn">显示职位</span>
          <input
            class="switch"
            type="checkbox"
            v-model="j.active"
            @change="switchCheck(j.id)"
          />
          <span class="hidden-btn">隐藏职位</span>
        </label>
      </div>
      <div class="preview" @click.stop="viewPos(j)">查看职位</div>
      <div class="receive" @click.stop="viewResume(j)">查看收到的简历</div>
      <div class="toggle">
        <span
          @click.stop="j.showTipsMenu = !j.showTipsMenu"
          class="el-icon-more"
        ></span>
        <div class="tips-menu" v-if="j.showTipsMenu">
          <p class="edit" @click.stop="jumpTo(j)">修改职位</p>
          <p class="delete" @click.stop="toDelete(j.id, i)">删除职位</p>
        </div>
      </div>
      <div class="pro">
        <span class="text">招聘截止: {{ j.end_time_cn }}</span>
        <div v-if="j.check != null" style="display: inline">
          <span class="text">审核状态: {{ j.check == 0 ? "待审核" : "不通过（原因：" + j.detail + "）" }}</span>
        </div>
        <span class="time">{{ j.addtime }} 发布</span>
      </div>
    </div>
    <client-only>
      <el-dialog
        width="445px"
        :visible.sync="showConfirm"
        :show-close="false"
        custom-class="dialog-confirm"
      >
        <Confirm
          v-if="showConfirm"
          :info-title="infoTitle"
          @confirm="confirm"
          @cancel="cancel"
        />
      </el-dialog>
    </client-only>
  </div>
</template>

<script>
import { jobToggleShow, jobDelete } from '@/api/job';

export default {
  props: ['list'],
  watch: {
    list (val) {
      this.jobList = val;
    }
  },
  data () {
    return {
      jobList: [],
      showTipsMenu: false,
      infoTitle: '确认删除吗？',
      deleteId: '',
      deleteIndex: null,
      showConfirm: false
    }
  },
  created () {
    this.jobList = this.list;
  },
  mounted () {

  },
  computed: {

  },
  methods: {
    switchCheck (id) { // switch
      jobToggleShow({
        id
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg || '切换成功！')
        } else {
          this.$message.error(res.msg);
        }
      }).catch(err => {
        console.error(err)
        this.$message.error(err);
      })
    },
    jumpTo (obj) {
      this.$router.push({
        path: '/position/detail',
        query: {id: obj.id}
      })
    },
    viewPos (obj) {
      this.$router.push({
        path: '/job/detail',
        query: { id: obj.id }
      })
    },
    viewResume (obj) {
      this.$router.push({
        path: '/resume?id=' + obj.id + '&resumeTitle=' + obj.name,
      })
    },
    toCloseMenu () { // 关闭 菜单
      this.jobList = this.jobList.map(s => {
        s.showTipsMenu = false;
        return s;
      })
    },
    toDelete (id, index) {
      this.deleteId = id;
      this.deleteIndex = index;
      this.showConfirm = true;
    },
    confirm () {
      jobDelete({
        id: this.deleteId
      }).then(res => {
        if (res.code === 200) {
          this.showConfirm = false;
          this.$message.success(res.msg || '切换成功！');
          this.jobList.splice(this.deleteIndex, 1);
        } else {
          this.$message.error(res.msg);
        }
      }).catch(err => {
        console.error(err)
        this.$message.error(err);
      })
    },
    cancel () {
      this.showConfirm = false;
    },
  },
}
</script>

<style lang="scss" scoped>
.job-list {
  margin: 20px auto;
  .job-item {
    height: 170px;
    width: 100%;
    border: 1px solid #ebebeb;
    background: #fff;
    margin-bottom: 10px;
    padding: 0 40px;
    cursor: pointer;
    &:hover {
      background: #f6f7f8;
    }
    & > div {
      float: left;
    }
    .job-pos {
      width: 340px;
      font-size: 18px;
      margin-top: 30px;
      .job-name {
        color: #242924;
        margin-right: 40px;
      }
      .money {
        color: $base-color-important;
      }
      .wrap {
        margin-top: 10px;
        p {
          margin-right: 30px;
          color: #999;
          line-height: 20px;
          font-size: 14px;
          display: inline-block;
        }
        i {
          vertical-align: middle;
          color: #999;
          margin-right: 5px;
        }
        span {
          vertical-align: middle;
          color: #999;
        }
      }
    }
    .resume-num {
      line-height: 115px;
      width: 210px;
      color: #24292f;
    }
    .toggle-switch {
      width: 280px;
      line-height: 115px;
      span {
        vertical-align: middle;
        color: #666;
        cursor: pointer;
      }
      input {
        margin: 0 10px;
        width: 44px;
        height: 22px;
        position: relative;
        border: 1px solid #ccc;
        background: #ccc;
        cursor: pointer;
        border-radius: 11px;
        background-clip: content-box;
        display: inline-block;
        vertical-align: middle;
        -webkit-appearance: none;
        &::before {
          content: '';
          width: 22px;
          height: 22px;
          box-sizing: border-box;
          position: absolute;
          top: -1px;
          left: 21px;
          border-radius: 50%;
          background: #fff;
          border: 1px solid $base-color;
          box-shadow: 0 1px 3px rgb(0 0 0 / 40%);
        }
        &:checked {
          border-color: $base-color;
        }
        &.switch {
          transition: border cubic-bezier(0, 0, 0, 1) 0.4s,
            box-shadow cubic-bezier(0, 0, 0, 1) 0.4s;
          &::before {
            transition: left 0.3s;
          }
          &:checked {
            box-shadow: $base-color 0 0 0 16px inset;
            background: $base-color;
            transition: border ease 0.4s, box-shadow ease 0.4s,
              background ease 1.2s;
            &::before {
              left: 0;
            }
          }
        }
      }
    }
    .preview {
      width: 90px;
      color: #24292f;
    }
    .receive {
      width: 140px;
    }
    .preview,
    .receive {
      line-height: 115px;
      &:hover {
        color: $base-color;
      }
    }
    .toggle {
      margin-top: 48px;
      position: relative;
      span {
        display: inline-block;
        width: 24px;
        height: 14px;
        line-height: 14px;
        text-align: center;
        border-radius: 4px;
        border: 1px solid #ccc;
        color: #ccc;
        font-size: 12px;
      }
      .tips-menu {
        position: absolute;
        top: 24px;
        left: -22px;
        width: 70px;
        line-height: 28px;
        background: #fff;
        border-radius: 2px;
        box-shadow: 0 0 4px rgba(0, 0, 0, 0.15);

        p {
          font-size: 12px;
          text-align: center;
        }
        .edit {
          color: #333;
          &:hover {
            color: $base-color;
          }
        }
        .delete {
          color: $base-color-danger;
          &:hover {
            color: $base-color-important;
          }
        }
      }
    }
    .pro {
      width: 100%;
      height: 50px;
      line-height: 50px;
      border-top: 1px solid #f6f7f8;
      .text {
        position: relative;
        padding-left: 15px;
        color: #999;
        height: 22px;
        line-height: 22px;
        font-size: 14px;
        &::before {
          content: '';
          height: 16px;
          width: 2px;
          background: #4771dc;
          position: absolute;
          left: 2px;
          top: 2px;
        }
      }
      .time {
        color: #999;
        float: right;
      }
    }
  }
}
</style>
